$(function () {
    layui.use(['form', 'layer'], function () {
        var form = layui.form(),
            layer = layui.layer;

        // 监听滚动
        $('#content').scroll(function () {
            if ($('#ReviewBtn')) {
                if ($('#scrollBackTop').hasClass('sr-only')) {
                    $('#ReviewBtn').css({ bottom: '60px' });
                } else {
                    $('#ReviewBtn').css({ bottom: '122px' });
                }
            }
        });

        $('#rollback').click(function () {
            layer.open({
                type: 1,
                id: 'rollback-modal',
                title: '撤回原因',
                area: ['400px'],
                content: [
                    '<div style="padding: 15px 15px 0 15px;">',
                    '<textarea id="rollback-reason" name="rollback_reason" class="form-control" rows="5" placeholder="请输入撤回原因"></textarea>',
                    '</div>'
                ].join(''),
                btn: ['确定', '取消'],
                yes: function(index, layero) {
                    var rollbackReason = $('#rollback-modal textarea[name=rollback_reason]').val();
                    if ($.trim(rollbackReason) == '') {
                        layer.msg('请输入撤回原因', {
                            icon: 2,
                            time: 2000
                        });
                        return;
                    }
                    var loading = layer.load(2, { // 创建loading层
                        shade: [0.5, '#fff'] //0.5透明度的白色背景
                    });
                    rollbackApproval(rollbackReason).then(function (response) {
                        if (response.errno) {
                            layer.closeAll();
                            layer.msg(response.message, { icon: 2, time: 3000 });
                        } else {
                            layer.msg(response.message, { icon: 1, time: 1500 });
                            setTimeout(function () {
                                location.reload();
                            }, 1500);
                        }
                    });
                }
            });
            return;

            layer.confirm("你确定要撤回该审批吗？", {
                title: '提示',
                icon: 3,
                btn: ['确定', '取消']
            }, function () {
                rollbackApproval().then(function (response) {
                    if (response.errno) {
                        layer.msg(response.message, { icon: 2, time: 3000 });
                    } else {
                        layer.msg(response.message, { icon: 1, time: 1500 });
                        setTimeout(function () {
                            location.reload();
                        }, 1500);
                    }
                });
            });
        });

        // 撤回审批
        function rollbackApproval(rollbackReason) {
            return new Promise(function (resolve, reject) {
                $.post('/approval/rollback', {
                    approval_id: approval_id,
                    rollback_reason: rollbackReason
                }, function (response) {
                    resolve(response);
                }, 'json');
            });
        }

        $('#ReviewBtn').click(function () {
            var approval_id = $('#approval-id').val();
            layer.open({
                type: 1,
                id: 'approval-modal',
                shade: [0.5, '#000000'],
                title: '审批',
                area: ['420px', '260px'], //宽高
                btn: ['同意', '拒绝'],
                content: $('.approval-modal-content').html(),
                yes: function yes(index, layero) {
                    var comment = $('#approval-modal #comment').val();
                    var loading = layer.load(2, { // 创建loading层
                        shade: [0.5, '#fff'] //0.5透明度的白色背景
                    });
                    $.post('/Approval/audit', { approval_id: approval_id, status: 1, comment: comment }, function (response) {
                        notify(response);
                    }, 'json');
                },
                btn2: function btn2(index, layero) {
                    var comment = $('#approval-modal #comment').val();
                    if (!comment) {
                        layer.msg('请输入拒绝理由');
                        return;
                    }
                    var loading = layer.load(2, { // 创建loading层
                        shade: [0.5, '#fff'] //0.5透明度的白色背景
                    });
                    $.post('/Approval/audit', { approval_id: approval_id, status: 0, comment: comment }, function (response) {
                        notify(response);
                    }, 'json');
                }
            });
        });

        function notify(response) {
            if (response.errno) {
                layer.msg(response.msg, { icon: 2, time: 3000 });
                setTimeout(function () {
                    location.reload();
                }, 1500);
            } else {
                layer.msg(response.msg, { icon: 1, time: 1500 });
                setTimeout(function () {
                    location.reload();
                }, 1500);
            }
        }
    });
});